import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '@/store/index'
import utils from '@/utils/utils'

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: '/layout'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/layout',
    name: 'layout',
    component: () => import('../components/Layout.vue'),
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: 'welcome',
        meta: { title: '欢迎体验Vue3全栈课程' },
        component: () => import('../views/Welcome.vue'),
      },
      // {
      //   path: '/system/user',
      //   name: 'user',
      //   meta: { title: '用户管理' },
      //   component: () => import('../views/User.vue'),
      // },
      // {
      //   path: '/system/menu',
      //   name: 'menu',
      //   meta: { title: '菜单管理' },
      //   component: () => import('../views/Menu.vue'),
      // },
      // {
      //   path: '/system/role',
      //   name: 'role',
      //   meta: { title: '角色管理' },
      //   component: () => import('../views/Role.vue'),
      // },
      // {
      //   path: '/system/dept',
      //   name: 'dept',
      //   meta: { title: '部门管理' },
      //   component: () => import('../views/Dept.vue'),
      // },
      // {
      //   path: '/audit/leave',
      //   name: 'leave',
      //   meta: { title: '休假申请' },
      //   component: () => import('../views/Leave.vue'),
      // },
      // {
      //   path: '/audit/approve',
      //   name: 'approve',
      //   meta: { title: '待审批' },
      //   component: () => import('../views/Approve.vue'),
      // },
    ]
  },
  {
    path: '/404',
    name: '404',
    component: () => import('../views/404.vue'),
  },
];




const router = new VueRouter({
  routes,
});

function _import(view) {
  return () => import('../views/' + view + '.vue')
}

// 动态加载路由方法
function getMenuList() {
  // 判断是否有token,用户是否登录
  let token = store.state.token

  // 获取到菜单数据
  let menuList = store.state.menuList

  // 将menuList处理成一维数组
  let route = utils.generateRouters(menuList)

  // 动态的将组件的路由添加到路由表里面
  route.forEach((item) => {
    item.component = _import(item.component)
    router.addRoute('layout', item)
  })
}

getMenuList()


export default router;
